completed trees - 3#1590
Conversation
There was a problem hiding this comment.
Pull request overview
Adds Java binary-tree path-sum solution code (DFS + backtracking) to the repository.
Changes:
- Added a
pathSum(Path Sum II) implementation using recursion, path tracking, and backtracking. - Added the same implementation in two separate files (
symmetrictree.javaandpathsum2.java).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| symmetrictree.java | Adds a DFS/backtracking pathSum solution (currently duplicated and file name does not match implementation). |
| pathsum2.java | Adds a DFS/backtracking pathSum solution (same implementation as symmetrictree.java). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * } | ||
| * } | ||
| */ | ||
| class Solution { |
| public List<List<Integer>> pathSum(TreeNode root, int targetSum) { | ||
| this.result = new ArrayList<>(); | ||
| helper(root, targetSum, 0, new ArrayList<>()); | ||
| return result; |
| // Time Complexity : O(n) | ||
| // Space Complexity : O(n) - for the recursion stack | ||
| // Did this code successfully run on Leetcode : yes | ||
| // Any problem you faced while coding this : yes writing logic for backtracking and keeping track of the current sum and path | ||
|
|
| // Time Complexity : O(n) | ||
| // Space Complexity : O(n) - for the recursion stack | ||
| // Did this code successfully run on Leetcode : yes | ||
| // Any problem you faced while coding this : local and global variables were not being updated correctly, which was giving wrong results. After fixing that, it worked fine. | ||
|
|
| public List<List<Integer>> pathSum(TreeNode root, int targetSum) { | ||
| this.result = new ArrayList<>(); | ||
| helper(root, targetSum, 0, new ArrayList<>()); | ||
| return result; |
| public List<List<Integer>> pathSum(TreeNode root, int targetSum) { | ||
| this.result = new ArrayList<>(); | ||
| helper(root, targetSum, 0, new ArrayList<>()); | ||
| return result; |
Path Sum II (pathsum2.java)Strengths:
Areas for Improvement:
Minor Note: VERDICT: PASS Symmetric Tree (symmetrictree.java)The code quality itself is good - the backtracking is implemented correctly, comments are clear, and the solution follows good practices for the Path Sum II problem. However, you've solved the wrong problem entirely. Key issues:
To solve the Symmetric Tree problem correctly:
VERDICT: NEEDS_IMPROVEMENT |
No description provided.